package com.rub.android.utils

import com.blankj.utilcode.util.ThreadUtils

class Task<T>(private val doInBackground: (() -> (T?))?, private val callback: ((T?) -> Unit)?) :
    ThreadUtils.Task<T>() {
    override fun doInBackground(): T? {
        try {
            return doInBackground?.invoke()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return null
    }

    override fun onSuccess(result: T) {
        try {
            callback?.invoke(result)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onFail(t: Throwable?) {
        try {
            callback?.invoke(null)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onCancel() {
        try {
            callback?.invoke(null)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}